package com.zxt.dental_sys.Controller.Consult;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.zxt.dental_sys.model.ConsultInfo;
import com.zxt.dental_sys.model.vo.RestBean;
import com.zxt.dental_sys.service.impl.ConsultInfoServiceImpl;
import com.zxt.dental_sys.service.impl.SysElementUserinfoServiceImpl;
import com.zxt.dental_sys.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/consult")
public class ConsultController {

    @Resource
    ConsultInfoServiceImpl consultInfoService;

    @Resource
    JwtUtil jwtUtil;
    @Autowired
    private SysElementUserinfoServiceImpl sysElementUserinfoServiceImpl;

    @RequestMapping("ask")
    public RestBean askConsult(@RequestBody ConsultInfo consultInfo, HttpServletRequest request){
        String uuid = jwtUtil.getJwtUUID(request);
        consultInfo.setPatientId(uuid);
        consultInfo.setInputTime(new Date());
        consultInfo.setConsultId(UUID.randomUUID().toString());
        return consultInfoService.save(consultInfo)?
                RestBean.success(consultInfo):RestBean.failure(500,"server error");
    }

    @RequestMapping("/list")
    public RestBean getAll(String  name){
        List<ConsultInfo> list = consultInfoService.list(new QueryWrapper<ConsultInfo>()
                .eq("is_deleted",0)
                .like("content",name)
                .orderByDesc("input_time")
                .orderByDesc("update_time")
        );
        return RestBean.success(list);
    }

    @RequestMapping("/doctor/getData/{page}/{size}")
    public RestBean doctorGetData(@PathVariable int page,@PathVariable int size){
        PageMethod.startPage(page,size);
        List<ConsultInfo> list = consultInfoService.list(new QueryWrapper<ConsultInfo>()
                .eq("is_deleted", 0)
//                .isNull("forward_content")
                .orderByDesc("input_time")
        );
        return RestBean.success(new PageInfo(list));
    }

    @RequestMapping("/forwardInfo")
    public RestBean forwardQ(@RequestBody ConsultInfo consultInfo,HttpServletRequest request){
        try {
            String uuid = jwtUtil.getJwtUUID(request);
            if (uuid == null){
                return RestBean.failure(400,"用户异常");
            }
            ConsultInfo byId = consultInfoService.getById(consultInfo);
            byId.setForwardContent(consultInfo.getForwardContent());
            byId.setDoctorId(uuid);
            byId.setUpdateTime(new Date());
            return consultInfoService.updateById(byId)?
                    RestBean.success(byId):RestBean.failure(500,"server error");
        } catch (Exception e) {
            return  RestBean.failure(400,"记录异常");
        }
    }

    @RequestMapping("/lists/{page}/{size}")
    public RestBean list(@PathVariable int page , @PathVariable int size,String name){
        PageMethod.startPage(page,size);
        List<ConsultInfo> list = consultInfoService.list(new QueryWrapper<ConsultInfo>()
                .eq("is_deleted", 0)
                .like("content", name)
                .orderByDesc("input_time")
                .orderByDesc("update_time")
        );
        list.forEach(el->{
            el.setPatientId(JSON.toJSONString(sysElementUserinfoServiceImpl.getById(el.getPatientId())));
            el.setDoctorId(el.getDoctorId() == null?"":JSON.toJSONString(sysElementUserinfoServiceImpl
                    .getById(el.getDoctorId())));
        });
        return RestBean.success(new PageInfo(list));
    }


}
